<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="合作社或扶贫公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'householdName', validatorRules.householdName]" placeholder="请输入合作社或扶贫公司名称"></a-input>
        </a-form-item>

        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['hzsType',validatorRules.hzsType]" :trigger-change="true"
                             dictCode="hzs_type" placeholder="请选择合作社类型"/>
        </a-form-item>

        <!--  <a-form-item label="乡镇" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fwzId']" :trigger-change="true" dictCode=" tb_jrtx_fwz, township_name,id     " placeholder="请选择乡镇"/>
        </a-form-item>
          
      <a-form-item label="自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fwbId']" :trigger-change="true" dictCode="tb_jrtx_fwb,service_name,id" placeholder="请选择自然村"/>
        </a-form-item>-->

        <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'address', validatorRules.address]" placeholder="请输入地址"></a-input>
        </a-form-item>

        <a-form-item label="法人代表" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'legalRepresentative', validatorRules.legalRepresentative]"
                   placeholder="请输入法人代表"></a-input>
        </a-form-item>

        <a-form-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'phone', validatorRules.phone]" placeholder="请输入电话"></a-input>
        </a-form-item>

        <!--<a-form-item label="是否有对公账户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['isAccount']" :trigger-change="true" dictCode="is_account" placeholder="请选择是否有对公账户"/>
        </a-form-item>
          
        <a-form-item label="账户名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'username', validatorRules.username]" placeholder="请输入账户名"></a-input>
        </a-form-item>
          
        <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'bank', validatorRules.bank]" placeholder="请输入开户行"></a-input>
        </a-form-item>
          
        <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'account', validatorRules.account]" placeholder="请输入账号"></a-input>
        </a-form-item>-->

        <!-- <a-form-item label="是否使用贷款" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-dict-select-tag type="list" v-decorator="['isLoans']" :trigger-change="true" dictCode="is_loans" placeholder="请选择是否使用贷款"/>
         </a-form-item>

         <a-form-item label="已使用贷款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-input v-decorator="[ 'loansMoney', validatorRules.loansMoney]" placeholder="请输入已使用贷款金额"></a-input>
         </a-form-item>-->

        <a-form-item label="带动贫困户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'assistedPoorHouseholds', validatorRules.assistedPoorHouseholds]"
                   placeholder="请输入带动贫困户数"></a-input>
        </a-form-item>

        <!--<a-form-item label="参加过扶贫项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['povertyAlleviationProjects']" rows="4" placeholder="请输入参加过扶贫项目"/>
        </a-form-item>
          
        <a-form-item label="实施过产业项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['industrialProjects']" rows="4" placeholder="请输入实施过产业项目"/>
        </a-form-item>-->


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "CooperativeModal",
        components: {
            JDictSelectTag,
        },
        data() {
            return {
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    phone:{rules: [{ required: true, message: '电话不能为空'}]},
                    householdName: {rules: [{ required: true, message: '名称不能为空'}]},
                    address:{rules: [{ required: true, message: '地址不能为空'}]},
                    fwzId: {},
                    fwbId: {},
                    legalRepresentative: {rules: [{ required: true, message: '法人代表不能为空'}]},
                    isAccount: {},
                    username: {},
                    bank: {},
                    account: {},
                    isLoans: {},
                    hzsType: {rules: [{ required: true, message: '类型不能为空'}]},
                    loansMoney: {},
                    povertyAlleviationProjects: {},
                    industrialProjects: {},
                    assistedPoorHouseholds: {},
                },
                url: {
                    add: "/industrysupport/cooperative/add",
                    edit: "/industrysupport/cooperative/edit",
                }

            }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'householdName', 'hzsType', 'fwzId', 'fwbId', 'legalRepresentative', 'isAccount', 'username', 'bank', 'account', 'isLoans', 'loansMoney', 'assistedPoorHouseholds', 'povertyAlleviationProjects', 'industrialProjects', 'address', 'phone'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                                that.$emit('parentComponentReLoad', 'AA')
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'householdName', 'hzsType', 'fwzId', 'fwbId', 'legalRepresentative', 'isAccount', 'username', 'bank', 'account', 'isLoans', 'loansMoney', 'assistedPoorHouseholds', 'povertyAlleviationProjects', 'industrialProjects', 'address', 'phone'))
            }

        }
    }
</script>